package plugin.demo.runner.apt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sun.tools.javac.processing.JavacFiler;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import plugin.demo.runner.apt.boot.bean.User;
import plugin.demo.runner.apt.config.AptRunnerConfigurationProperties;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/5/13 下午3:30
 * @UpdateDate:
 * @Description:
 */
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class AptRunnerApplication implements ApplicationContextAware,CommandLineRunner, EnvironmentAware {

    ApplicationContext applicationContext;
    Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(AptRunnerApplication.class,args);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void run(String... args) throws Exception {
        AptRunnerConfigurationProperties bean = applicationContext.getBean(AptRunnerConfigurationProperties.class);
        if(bean != null){
            String s = JSON.toJSONString(bean, SerializerFeature.PrettyFormat);
            System.out.println(s);
        }
        try {
            User user = applicationContext.getBean(User.class);
            System.out.println(JSON.toJSONString(user));
        }catch (Throwable e){
            e.printStackTrace();
        }
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
